<html>
    <head>
        <link type="text/css" href="Libs/jquery-ui-1.8.13.custom.css" rel="Stylesheet" />	
        <script type="text/javascript" src="Libs/jquery-1.6.4.js"></script>
        <script type="text/javascript" src="Libs/jquery-ui-1.8.13.custom.min.js"></script>

        <script type="text/javascript" src="Libs/webgl-utils.js"></script>
        <script type="text/javascript" src="Libs/glMatrix-0.9.5.min.js"></script>

        <script type="text/javascript" src="./gl.js"></script>

		<!-- See audio.js file for credit info -->
		<script type="text/javascript" src="./audio.js"></script>
		<!-- See audio.js file for credit info -->
		
        <script type="text/javascript" src="Models/model.js"></script>
        <script type="text/javascript" src="Models/BallModel.js"></script>
        <script type="text/javascript" src="Models/RectModel.js"></script>
        <script type="text/javascript" src="Models/RampModel.js"></script>
        <script type="text/javascript" src="Models/SegmentedRectModel.js"></script>
		<script type="text/javascript" src="Models/HamsterBallModel.js"></script>

		<script type="text/javascript" src="Models/hamsterball.js"></script>
		
        <script type="text/javascript" src="./game.js"></script>
        <script type="text/javascript" src="./util.js"></script>
        <script type="text/javascript" src="./physics.js"></script>
        <script type="text/javascript" src="./graphics.js"></script>
        <script type="text/javascript" src="./setup.js"></script>
		<script type="text/javascript" src="./levels.js"></script>


        <script id="normalshader-fs" type="x-shader/x-fragment">
            #ifdef GL_ES
            precision highp float;
            #endif

            varying vec4 vColor;
            
            void main(void) {
                gl_FragColor = vColor;
            }
        </script>

        <script id="normalshader-vs" type="x-shader/x-vertex">
            attribute vec3 aVertexPosition;
            attribute vec4 aVertexColor;
            attribute vec3 aVertexNormal;
            
            uniform mat4 uMVMatrix;
            uniform mat4 uPMatrix;
			uniform vec4 aColor;
			uniform vec3 lightUniform;
			
            varying vec4 vColor;
            
            void main(void) {
                gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
				vec4 pos = uMVMatrix * vec4(aVertexPosition, 1.0);
                vec3 light = vec3(lightUniform[0] - pos[0], 
									lightUniform[1] - pos[1],
									lightUniform[2] - pos[2]);
				light = normalize(light);
                vec4 ta = normalize(uMVMatrix * vec4(aVertexNormal, 0.0));
                vec3 a = vec3(ta[0], ta[1], ta[2]);
				vec3 b = vec3(light[0] - a[0], light[1] - a[1], light[2] - a[2]);
				
				
                float prod = pow(max(0.0, dot(a, light)), 0.8);
                //prod = 0.318309886 * prod;
				
                vColor = vec4(	prod * aColor[0],
                                prod * aColor[1],
                                prod * aColor[2],
                                aColor[3]);
            }
        </script>
		
		<script id="watershader-fs" type="x-shader/x-fragment">
            #ifdef GL_ES
            precision highp float;
            #endif

            varying vec4 vColor;
			varying vec4 pos;

			varying vec3 normal;
			varying vec3 lightPos;
            
            void main(void) {
                vec3 light = vec3(-lightPos[0] + pos[0], 
									-lightPos[1] + pos[1],
									-lightPos[2] + pos[2]);

				light = normalize(light);
				vec3 norm = normal;
				norm = normalize(norm);

                float prod = max(0.0, min(1.0, dot(norm, light)));

				float af = 0.2, df = 1.0 - af;
				gl_FragColor = vec4(
					vColor[0] * af + df * prod * vColor[0],
					vColor[1] * af + df * prod * vColor[1],
					vColor[2] * af + df * prod * vColor[2],
					1.0);
            }
        </script>

		
        <script id="watershader-vs" type="x-shader/x-vertex">
            attribute vec3 aVertexPosition;
            attribute vec3 aVertexNormal;
            attribute vec3 aVertexExtra;
            
            uniform mat4 uMVMatrix;
            uniform mat4 uPMatrix;
			uniform vec4 aColor;
			uniform vec4 extraUniform;
            uniform float uTime;
			uniform vec3 lightUniform;
			uniform mat4 uCollisions;
			

            varying vec4 vColor;
			varying vec4 pos;

			varying vec3 normal;
			varying vec3 lightPos;

			float pi = 3.141592653589793238462643383279;

			float dist(vec4 a, vec4 b) {
				return sqrt((a[0] - b[0]) * (a[0] - b[0]) + 16.0 * (b[2]) * (b[2]));
			}

			vec4 standing(vec4 p) {
				float xp = (p[0] + uTime / 40.0) / 24.0;
				p[1] += 4.0 * cos(xp);
				return p;
			}

			vec4 collide(vec4 collision, vec4 p) {
				if (collision[3] > 0.01) {
					float dt = (uTime - collision[2]);
					float dx = dist(collision, p);
					float d = dx / 10.0 - dt / 80.0;
					if (d < 0.0) {
						float off = max(0.0, collision[3] * 1.2 * (1.0 - abs(d) / 16.0));
						if (dx < 10.0)
							off *= dx / 10.0;
						off *= max(0.0, (5000.0 - dt) / 5000.0);
						p[1] += off * -sin(d);
					}
				}
				return p;
			}

			vec4 calculate(vec4 p) {
				vec4 ret = standing(p);
				ret = collide(uCollisions[0], ret);
				ret = collide(uCollisions[1], ret);
				ret = collide(uCollisions[2], ret);
				ret = collide(uCollisions[3], ret);
				return ret;
			}

			vec3 crossP(vec4 a, vec4 b) {
				return vec3(
					a[1] * b[2] - a[2] * b[1],
					a[2] * b[0] - a[0] * b[2],
					a[0] * b[1] - a[1] * b[0]
					);
			}

            void main(void) {
                pos = uMVMatrix * vec4(aVertexPosition, 1.0);
				if (aVertexExtra[0] > 0.0) {
					pos = calculate(pos);
				}
				if (aVertexExtra[1] > 0.0) {
					vec4 left = vec4(pos);
					vec4 right = vec4(pos);
					vec4 back = vec4(pos);
					vec4 forw = vec4(pos);
					left[0] += 1.0;
					right[0] -= 1.0;
					back[2] += 0.2;
					forw[2] -= 0.2;
					left = calculate(left);
					right = calculate(right);
					back = calculate(back);
					forw = calculate(forw);
					normal = crossP(back - forw, right - left);
				} else {
					vec4 norm4 = uMVMatrix * vec4(aVertexNormal, 0.0);
					normal = vec3(norm4[0], norm4[1], norm4[2]);
				}
				lightPos = lightUniform;
				vColor = aColor;
                gl_Position = uPMatrix * pos;
            }

        </script>
		
		<script id="textureshader-fs" type="x-shader/x-fragment">
		  varying highp vec2 vTextureCoord;  
			
		  uniform sampler2D uSampler;  
			
		  void main(void) {  
			gl_FragColor = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t));  
		  } 
        </script>

        <script id="textureshader-vs" type="x-shader/x-vertex">
		  attribute vec3 aVertexPosition;  
		  attribute vec2 aTextureCoord;  
		  
		  uniform mat4 uMVMatrix;  
		  uniform mat4 uPMatrix;  
			
		  varying highp vec2 vTextureCoord;  
		  
		  void main(void) {  
			gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);  
			vTextureCoord = aTextureCoord;  
		  }  
        </script>
		
		
		<script id="objectshader-fs" type="x-shader/x-fragment">
            #ifdef GL_ES
            precision highp float;
            #endif

			varying mat4 collisions;

            varying vec4 vColor;
			varying vec4 collisionSide;

			// x, y, time, velocity
			varying vec3 extraData;
			varying vec3 position;

			varying float currTime;
			
			float dist2(float x1, float y1, float z1, float x2, float y2, float z2) {
				return sqrt( pow(x1-x2,2.0) + pow(y1-y2,2.0) + pow(z1-z2,2.0) );
			}

			vec4 collide(vec4 collision, vec4 color) {
				float change = 0.0;
				float maxF = 10.0;
				float radius = 30.0;
				vec4 targetColor = vec4(1.0, 0.0, 0.0, 1.0);

				float dist = dist2( position.x, position.y, position.z, collision.x, collision.y, float(0.0));

				if( dist < radius && currTime > collision[2]) {
					change = (radius - dist)  / radius;
					change = change * ( collision[3] / maxF);

					if ( currTime - collision[2] > 2000.0) {
						change = 0.0;
					}
					else {
						change = change * (( 2000.0 - (currTime - collision[2])) / 2000.0 );
					}


					vec4 diff = targetColor - color;						
					diff *= vec4(change, change, change, 1.0);						
					return color + diff;
				}
				return color;
			}
            
            void main(void) {			
				gl_FragColor = collide(collisions[0], vColor);
				gl_FragColor = collide(collisions[1], gl_FragColor);
				gl_FragColor = collide(collisions[2], gl_FragColor);
				gl_FragColor = collide(collisions[3], gl_FragColor);
            }
        </script>

        <script id="objectshader-vs" type="x-shader/x-vertex">
            attribute vec3 aVertexPosition;
            attribute vec4 aVertexColor;
            attribute vec3 aVertexNormal;
			attribute vec3 aVertexExtra;
			
            
            uniform mat4 uMVMatrix;
            uniform mat4 uPMatrix;
			uniform mat4 uCollisions;

			uniform vec4 aColor;
			uniform vec4 uCollisionSide;
			
			uniform vec3 lightUniform;
			
			uniform float uTime;
			//uniform vec4 uExtraData;
			
			varying mat4 collisions;

			varying vec4 collisionSide;
            varying vec4 vColor;

			varying vec3 extraData;
			varying vec3 position;
			
			varying float currTime;
			

            
            void main(void) {
                gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
				vec4 pos = uMVMatrix * vec4(aVertexPosition, 1.0);
                vec3 light = vec3(lightUniform[0] - pos[0], 
									lightUniform[1] - pos[1],
									lightUniform[2] - pos[2]);
				light = normalize(light);
                
				
				
                vec4 ta = normalize(uMVMatrix * vec4(aVertexNormal, 0.0));
                vec3 a = vec3(ta[0], ta[1], ta[2]);
				vec3 b = vec3(light[0] - a[0], light[1] - a[1], light[2] - a[2]);
				
				
                float prod = pow(max(0.0, dot(a, light)), 0.8);
                //prod = 0.318309886 * prod;
				
                vColor = vec4(	prod * aColor[0],
                                prod * aColor[1],
                                prod * aColor[2],
                                aColor[3]);
								
								
									
				position = vec3(pos[0], pos[1], pos[2]);

				//currTime = 1500.0;
				
				//position = uPosition;
				collisions = uCollisions;
				currTime = uTime;
				collisionSide = uCollisionSide;
				//extraData = aVertexExtra;	
					
				
            }
        </script>


    </head>

    <body>
		<div id="2dGame" style="position:absolute; top:0px;left:0px;display:none;">
			<canvas id="gameCanvas" class="boxed" width="1280" height="640"></canvas>
		</div>

		<div id="3dGame" style="position:absolute; top:0px;left:0px;">
			<canvas id="canvas" style="border: none;" width="1280" height="640"></canvas>    
		</div>

		<div id="gameInfoDiv" style="position:absolute; top:0px;left:0px;">
			<canvas id="gameInfo" style="border: none;" width="1280" height="640"></canvas>    
		</div>

		<div id="levelInfo" style="position:absolute; top:250px; left:250px;">
			
		</div>
		
    </body>

</html>

